
import 'element-ui/lib/theme-chalk/index.css'
import './Utils'
import './Utils'

import {randomText} from './Utils'


let Base64 = require('js-base64').Base64;
// 七牛云的图片外链地址
const qiniuaddr = "https://supimage.miniprogramhacker.cn"
// === 移除  ===//
function beforeRemove(file, fileList) {
  return this.$confirm(`确定移除 ${ file.name }？`);
}
function handleRemove(file, fileList, urlList) {
  alert(123)
  var _tmp = urlList;
  for (var i = 0, len = _tmp.length; i < len; i++) {
    if (_tmp[i].qiniuName == file.qiniuName) {
      _tmp.splice(i, 1);
      break;
    }
  }
  urlList = _tmp
  return urlList;
}

function handleExceed(files, fileList, count) {
  this.$message.warning(
    `图片数量超出了限制，如需更换，请删除上一张图片在重新选择！`
  );
}
// === 上传  ===//
function beforeAvatarUpload(file, urlList,key) {

  const isPNG = file.type === 'image/png'
  const isJPEG = file.type === 'image/jpeg'
  const isJPG = file.type === 'image/jpg'
  const isLt20M = file.size / 1024 / 1024 < 20

  if (!isPNG && !isJPEG && !isJPG) {
    this.$message.error('上传头像图片只能是 jpg、png、jpeg 格式!')
    return false
  }
  if (!isLt20M) {
    this.$message.error('上传头像图片大小不能超过 2MB!')
    return false
  }


  urlList.push({
    qiniuName: key
  });
  return urlList;
}

function uploadSuccess(response, file, fileList, urlList) {

  var picUrl = qiniuaddr + '/' + response.key;
  file.qiniuName = response.key;

  var _tmp = urlList;

  for (let i = 0, len = _tmp.length; i < len; i++) {
    var map = _tmp[i]
    if (map.qiniuName == response.key){
      map.url = picUrl
      map.response = response
    }
  }
  // urlList = _tmp
  return _tmp

}
function uploadError(err, file, fileList) {
  this.$message({
    message: "上传出错，请重试！",
    type: "error",
    center: true
  });
}

export default {
  // Vue.js的插件应当有一个公开方法 install。这个方法的第一个参数是 Vue 构造器，第二个参数是一个可选的选项对象。
  install: function (Vue) {
    Vue.prototype.beforeRemove = (file, fileList) => beforeRemove(file, fileList)
    Vue.prototype.handleRemove = (file, fileList, urlList) => handleRemove(file, fileList, urlList)
    Vue.prototype.handleExceed = (files, fileList, count) => handleExceed(files, fileList, count)
    Vue.prototype.beforeAvatarUpload = (file, urlList,key) => beforeAvatarUpload(file, urlList,key)
    Vue.prototype.uploadSuccess = (response, file, fileList, urlList) => uploadSuccess(response, file, fileList, urlList)
    Vue.prototype.uploadError = (err, file, fileList) => uploadError(err, file, fileList)

  }
}


